﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;

namespace SecuritySystem
{
    public partial class AddSubTypeForm : Form
    {
        List<ProductType> productTypeList;
        List<ProductSubType> productSubTypeList;
        TIEntities dbContext;
        public AddSubTypeForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            FillTypeCombo();
        }
        private void FillTypeCombo()
        {

            productTypeList = (from pt in dbContext.ProductTypes select pt).Distinct().ToList();
            if (productTypeList != null)
            {
                BindDataWithTypeCombo(productTypeList);
            }
            else
            {
                productTypeList = new List<ProductType>();
            }

        }
        private void BindDataWithTypeCombo(List<ProductType> productTypeList)
        {
          
            cmbBoxType.DataSource = null;
            cmbBoxType.DisplayMember = "Name";
            cmbBoxType.ValueMember = "ID";
            cmbBoxType.DataSource = productTypeList;
            cmbBoxType.Refresh();
        }
        private void btnAddNewSubType_Click(object sender, EventArgs e)
        {
            string subtypeName = textBoxNewSubType.Text.Trim();
            if (!string.IsNullOrWhiteSpace(subtypeName))
            {
                ProductSubType productSubType;
                int selectedTypeId = Convert.ToInt32(cmbBoxType.SelectedValue.ToString());
                productSubType = (from p in dbContext.ProductSubTypes where p.Name == subtypeName && p.ProductTypeID == selectedTypeId select p).FirstOrDefault();
                if (productSubType == null)
                {
                    productSubType = new ProductSubType();
                    productSubType.Name = subtypeName;
                    productSubType.ProductTypeID = selectedTypeId;
                    dbContext.AddToProductSubTypes(productSubType);
                    dbContext.SaveChanges();

                    productSubTypeList.Add(productSubType);

                    textBoxNewSubType.Clear();
                    BindDataWithSubTypeListBox(productSubTypeList);

                }
                else
                {
                    MessageBox.Show("Type already present in database.");
                }

            }
            else
            {
                MessageBox.Show("Invalid Type Name.");
            }
        }

        private void cmbBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBoxType.SelectedIndex > -1)
            {
                int selectedTypeId = Convert.ToInt32(cmbBoxType.SelectedValue.ToString());
                productSubTypeList = (from pt in dbContext.ProductSubTypes where pt.ProductTypeID == selectedTypeId select pt).Distinct().ToList();
                BindDataWithSubTypeListBox(productSubTypeList);

            }
        }
        private void BindDataWithSubTypeListBox(List<ProductSubType> productSubTypeList)
        {

            listBoxSubTypes.DataSource = null;
            listBoxSubTypes.DisplayMember = "Name";
            listBoxSubTypes.ValueMember = "ID";
            listBoxSubTypes.DataSource = productSubTypeList;
            
        }

        private void AddSubTypeForm_Load(object sender, EventArgs e)
        {

        }
    }
}
